// import xlsx from 'node-xlsx';
let fs = require('fs')
let async = require('async')
let xlsx = require('node-xlsx')
let AV = require('leanengine');
let moment = require('moment')
var common = require('../cloud/common');

exports.saveXlsxFromShopGoodType = async function () {
    let query = new AV.Query('Shop_good_type');
    query.limit(1000);
    query.find().then(res =>{
        let data = []
        for (let item of res){
            let ds = []
            ds.push(item.id)
            ds.push(item.get("name"))
            ds.push(item.get("type"))
            ds.push(item.get("level"))
            ds.push(item.get("status"))
            ds.push(item.get("code"))
            ds.push(item.get("fatherCode"))
            ds.push(item.get("sortNum"))
            ds.push(item.get("parentId"))
            data.push(ds)
        }
        buildXLSX(data)

    })
}
function buildXLSX(unauthorizedList) {
    let res = [{
        name: '数据处理结果',
        data: unauthorizedList

    }]
    let r = JSON.parse(JSON.stringify(res))
    console.log(r)
    const result = xlsx.build(r);
    fs.writeFileSync(`${__dirname}/shop_goods_type.xlsx`, result, 'binary')

    console.log('内容已经存放到xlsx里面')

}


exports.inputXlsxFromShopGoodType = async function () {

    const buffer = fs.readFileSync(`${__dirname}/shop_goods_type.xlsx`)
    const result = JSON.parse(JSON.stringify(xlsx.parse(buffer)));
    let list = result[0].data.slice(1)
    console.log(list)
    let ShopGoodType = AV.Object.extend("Shop_good_type")
    let dataArray = []
    for (let item of list){
        let obj = new ShopGoodType()
        obj.objectId = item[0]
        obj.set('name',item[1])
        obj.set('type',item[2])
        obj.set('level',item[3])
        obj.set('status',item[4])
        obj.set('code',item[5])
        obj.set('fatherCode',item[6])
        obj.set('sortNum',item[7])
        obj.set('parentId',item[8])


        dataArray.push(obj)

    }
    AV.Object.saveAll(dataArray).then(rs=>{
        // console.log(rs)
    }).catch(error=>{
        console.log(error)
    })



}

